<template>
  <el-form label-width="100px">
    <el-form-item label="SKU名称">
      <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input
        placeholder="SKU名称"
        type="number"
        v-model="skuParams.price"
      ></el-input>
    </el-form-item>
    <el-form-item label="重量(克)">
      <el-input
        placeholder="SKU名称"
        type="number"
        v-model="skuParams.weight"
      ></el-input>
    </el-form-item>
    <el-form-item label="SKU描述">
      <el-input
        type="textarea"
        placeholder="SKU描述"
        v-model="skuParams.skuDesc"
      ></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form :inline="true" label-width="100px">
        <el-form-item
          v-for="(item, index) in attrArr"
          :key="item.id"
          :label="item.attrName"
          style="margin-bottom: 6px"
        >
          <!-- 将选择的平台属性值作为当前item下的一个属性->最后保存时整理为对应的形式 -->
          <el-select style="width: 200px" v-model="item.attrIdAndValueId">
            <el-option
              :value="`${item.id}:${attrValue.id}`"
              v-for="(attrValue, index) in item.attrValueList"
              :key="attrValue.id"
              :label="attrValue.valueName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form :inline="true">
        <el-form-item
          v-for="(item, index) in saleArr"
          :key="item.id"
          :label="item.saleAttrName"
        >
          <el-select style="width: 200px" v-model="item.saleAttrIdAndValueId">
            <el-option
              :value="`${item.id}:${saleAttrValue.id}`"
              :label="saleAttrValue.saleAttrValueName"
              v-for="(saleAttrValue, index) in item.spuSaleAttrValueList"
              :key="saleAttrValue.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="imgArr" ref="table">
        <el-table-column type="selection" width="80px" align="center" />
        <el-table-column label="图片" align="center">
          <template #="{ row, $index }">
            <img :src="row.imgUrl" alt="" style="width: 100px" />
          </template>
        </el-table-column>
        <el-table-column
          label="名称"
          align="center"
          prop="imgName"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template #="{ row, $index }">
            <el-button type="warning" size="default" @click="handler(row)">
              设置默认
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="default" @click="save">保存</el-button>
      <el-button size="default" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import type { SpuData, SkuData } from '@/api/product/spu/type'
import { reactive, ref } from 'vue'
// 引入请求API
import { reqAttr } from '@/api/product/attr'
import { reqSpuImageList, reqHasSaleAttr, reqAddSku } from '@/api/product/spu'
import { ElMessage } from 'element-plus'
// 自定义事件的方法
let $emit = defineEmits(['changeScene'])
// 平台属性
let attrArr = ref<any>([])
// 销售属性
let saleArr = ref<any>([])
// 照片墙数据
let imgArr = ref<any>([])
// 标识table
let table = ref()
// 收集SKU的参数
let skuParams = reactive<SkuData>({
  category3Id: '',
  spuId: '',
  tmId: '',
  skuName: '',
  price: '',
  weight: '',
  skuDesc: '',
  skuAttrValueList: [],
  skuSaleAttrValueList: [],
  skuDefaultImg: '',
})
const cancel = () => {
  $emit('changeScene', { flag: 0, params: '' })
}
// 当前子组件的方法对外暴露
const initSkuData = async (
  c1Id: number | string,
  c2Id: number | string,
  spu: SpuData,
) => {
  // 收集数据
  skuParams.category3Id = spu.category3Id as number
  skuParams.spuId = spu.id as number
  skuParams.tmId = spu.tmId
  // 获取平台属性
  const result: any = await reqAttr(c1Id, c2Id, spu.category3Id)
  // console.log(result)
  // 获取销售属性
  const result1: any = await reqHasSaleAttr(spu.id as number)
  // console.log(result1)
  // 获取照片墙的数据
  const result2: any = await reqSpuImageList(spu.id as number)
  // console.log(result2)
  attrArr.value = result.data
  saleArr.value = result1.data
  imgArr.value = result2.data
}
// 设置默认图片的回调
const handler = (row: any) => {
  // 点击时，先让所有复选框不选中
  imgArr.value.forEach((item: any) => {
    table.value.toggleRowSelection(item, false)
  })
  // 复选框选中 只有其一选中
  table.value.toggleRowSelection(row, true)
  // 收集图片地址
  skuParams.skuDefaultImg = row.imgUrl
}
// 保存的回调
const save = async () => {
  let attrArrList = attrArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(':')
      prev.push({
        attrId,
        valueId,
      })
    }
    return prev
  }, [])
  skuParams.skuAttrValueList = attrArrList
  skuParams.skuSaleAttrValueList = saleArr.value.reduce(
    (prev: any, next: any) => {
      if (next.saleAttrIdAndValueId) {
        let [saleAttrId, saleAttrValueId] = next.saleAttrIdAndValueId.split(':')
        prev.push({
          saleAttrId,
          saleAttrValueId,
        })
      }
      return prev
    },
    [],
  )
  // 添加SKU的请求
  const result = await reqAddSku(skuParams)
  if (result.code == 200) {
    ElMessage({
      message: '添加SKU成功',
      type: 'success',
    })
    // 通知父组件切换场景0
    $emit('changeScene', { flag: 0, params: '' })
  } else {
    ElMessage({
      type: 'error',
      message: '添加SKU失败',
    })
  }
}
defineExpose({
  initSkuData,
})
</script>

<style scoped>
</style>